package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingOrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


import java.util.Date;



@Controller
@RequestMapping("/cargo/shippingOrder")
public class ShippingOrderController extends BaseController {
    @Reference
    private ShippingOrderService shippingOrderService;
    @Reference
    private PackingService packingService;


    @RequestMapping(value = "/list", name = "展示委托单数据")
    public String findPage(@RequestParam(name = "page", defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int size) {
        //查询委托单数据
        PageInfo pageInfo = shippingOrderService.findAll(getCompanyId(), pageNum, size);
        request.setAttribute("page", pageInfo);
        return "cargo/shippingOrder/shippingOrder-list";
    }

    @RequestMapping(value = "/toAdd", name = "进入添加委托单页面")
    public String toAdd(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        //需要查询已上报的装箱单数据
        PageInfo<Packing> list = packingService.findAlreadyUp(page, size, getCompanyId(), 1);
        request.setAttribute("page", list);
        return "cargo/shippingOrder/shippingOrder-add";
    }

    @RequestMapping(value = "/toUpdate",name = "进入修改页面")
    public String toUpdate(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        request.setAttribute("shippingOrder", shippingOrder);
        return "cargo/shippingOrder/shippingOrder-add";
    }

    @RequestMapping(value = "/edit", name = "添加委托单")
    public String edit(ShippingOrder shippingOrder, String id) {
        if (StringUtils.isEmpty(shippingOrder.getShippingOrderId())) {
            shippingOrder.setShippingOrderId(id);
            shippingOrder.setState(0);
            shippingOrder.setCreateBy(getUser().getUserName());
            shippingOrder.setCreateDept(getUser().getDeptId());
            shippingOrder.setCreateTime(new Date());
            shippingOrder.setCompanyId(getCompanyId());
            shippingOrder.setCompanyName(getCompanyName());
            shippingOrderService.save(shippingOrder);
        } else {
            shippingOrderService.update(shippingOrder);
        }
        return "redirect:/cargo/shippingOrder/list.do";
    }

    @RequestMapping(value = "/cancel", name = "取消委托单")
    public String cancel(String id) {
        shippingOrderService.cancel(id);
        return "redirect:/cargo/shippingOrder/list.do";
    }

    @RequestMapping(value = "/submit", name = "提交委托单")
    public String submit(String id) {

        shippingOrderService.submit(id);
        return "redirect:/cargo/shippingOrder/list.do";
    }

   @RequestMapping(value = "delete", name = "删除委托单")
   public String delete(String id) {
       shippingOrderService.deleteById(id);
       return "redirect:/cargo/shippingOrder/list.do";
   }


   @RequestMapping(value = "submit", name = "提交委托单")
   public String submit(String id) {
       ShippingOrder shippingOrder = shippingOrderService.findById(id);
      shippingOrder.setState(1);
       shippingOrderService.update(shippingOrder);
       Export export = exportService.findById(id);
       export.setState(2);
       exportService.update(export);
       return "redirect:/cargo/shippingOrder/list.do";
   }


    @RequestMapping(value ="/gangkou",name = "查看地图")
    public String gangkou(String id){
        HashMap<String, Object> map = new HashMap<>();
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        String portOfLoading = shippingOrder.getPortOfLoading();
        String portOfDischarge = shippingOrder.getPortOfDischarge();
        request.setAttribute("portOfLoading",portOfLoading);
        request.setAttribute("portOfDischarge",portOfDischarge);
//        map.put("portOfLoading",portOfLoading);
//        map.put("portOfDischarge",portOfDischarge);
//        request.setAttribute("map",map);
        return "forward:/map/map4.html";
    }

}
